﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
using System.Reflection;

namespace SoftballStats
{
    public partial class GamePage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Season"] != null)
            {
                // get season
                SoftballStats_BL.Season thisSeason = Session["Season"] as SoftballStats_BL.Season;
                lblCurrentSeason.Text = thisSeason.Description + " " + thisSeason.Year;

                //get team
                SoftballStats_BL.Team myTeam = new SoftballStats_BL.Team();
                if (Session["Team"] != null)
                {
                    myTeam = Session["Team"] as SoftballStats_BL.Team;
                }

                if (Request.QueryString["GameID"] != null)
                {
                    SoftballStats_BL.Game selectedGame = SoftballStats_BL.Game.GetGameByID(Guid.Parse(Request.QueryString["GameID"]));
                    if (selectedGame.ID != Guid.Empty)
                    {
                        if (myTeam.ID == Guid.Empty)
                        {
                            myTeam = SoftballStats_BL.Team.GetTeamByID(selectedGame.TeamID);
                        }

                        //fill game information, etc.
                        if (selectedGame.IsHome)
                        {
                            lblAwayTeam.Text = selectedGame.OpponentName;
                            lblAwayScore.Text = selectedGame.OpponentScore.ToString();
                            lblHomeTeam.Text = myTeam.Name;
                            lblHomeScore.Text = selectedGame.TeamScore.ToString();
                        }
                        else
                        {
                            lblAwayTeam.Text = myTeam.Name;
                            lblAwayScore.Text = selectedGame.TeamScore.ToString();
                            lblHomeTeam.Text = selectedGame.OpponentName;
                            lblHomeScore.Text = selectedGame.OpponentScore.ToString();
                        }

                        lblDate.Text = selectedGame.Date.ToString();
                        lblLocation.Text = selectedGame.Location;

                        if (selectedGame.IsWin)
                        {
                            lblAwayScore.ForeColor = System.Drawing.Color.DarkGreen;
                            lblAwayTeam.ForeColor = System.Drawing.Color.DarkGreen;
                            lblHomeScore.ForeColor = System.Drawing.Color.DarkGreen;
                            lblHomeTeam.ForeColor = System.Drawing.Color.DarkGreen;
                            lblDate.ForeColor = System.Drawing.Color.DarkGreen;
                            lblLocation.ForeColor = System.Drawing.Color.DarkGreen;
                            lblOutcome.Text = "W";
                            lblOutcome.ForeColor = System.Drawing.Color.DarkGreen;
                        }

                        GetHitters(selectedGame);
                        // TODO
                        //GetPitchers(selectedGame);
                    }
                    else
                    {
                        Response.Redirect("../Default.aspx");
                    }
                }
                else
                {
                    Response.Redirect("../Default.aspx");
                }
            }
            else
            {
                Response.Redirect("../SelectSeason.aspx");
            }

            //GetTeams();
        }

        private void GetPitchers(SoftballStats_BL.Game thisGame)
        {
            throw new NotImplementedException();
        }

        private void GetHitters(SoftballStats_BL.Game thisGame)
        {
            List<SoftballStats_BL.PlayerGame> lstPlayerGames = SoftballStats_BL.PlayerGame.GetPlayerGamesByGameID(thisGame.ID);

            for (int i = 0; i < lstPlayerGames.Count; i++)
            {
                HtmlTableRow tblRow = new HtmlTableRow();

                HtmlTableCell tblCell = new HtmlTableCell();
                Label lblBattingOrder = new Label();
                lblBattingOrder.Text = lstPlayerGames[i].BattingOrder.ToString();
                tblCell.Controls.Add(lblBattingOrder);
                tblRow.Cells.Add(tblCell);

                //get Player
                SoftballStats_BL.Player thisPlayer = SoftballStats_BL.Player.GetPlayerByID(lstPlayerGames[i].PlayerID);

                HtmlTableCell tblCell2 = new HtmlTableCell();
                LinkButton lnkName = new LinkButton();
                if (thisPlayer.LastName != "")
                {
                    lnkName.Text = thisPlayer.FirstName[0] + " " + thisPlayer.LastName + " " + lstPlayerGames[i].Position;
                }
                else
                {
                    lnkName.Text = thisPlayer.FirstName + " " + lstPlayerGames[i].Position;
                }
                lnkName.CommandName = lstPlayerGames[i].PlayerID.ToString();
                lnkName.CommandArgument = lstPlayerGames[i].ID.ToString();
                lnkName.Click += new EventHandler(this.lnkName_Click);
                tblCell2.Controls.Add(lnkName);
                tblRow.Cells.Add(tblCell2);

                HtmlTableCell tblCell3 = new HtmlTableCell();
                Label lblHAB = new Label();
                lblHAB.Text = lstPlayerGames[i].Hits.ToString() + "/" + lstPlayerGames[i].AtBats.ToString();
                tblCell3.Controls.Add(lblHAB);
                tblRow.Cells.Add(tblCell3);

                HtmlTableCell tblCell4 = new HtmlTableCell();
                Label lblRuns = new Label();
                lblRuns.Text = lstPlayerGames[i].Runs.ToString();
                tblCell4.Controls.Add(lblRuns);
                tblRow.Cells.Add(tblCell4);

                HtmlTableCell tblCell5 = new HtmlTableCell();
                Label lblSingles = new Label();
                lblSingles.Text = lstPlayerGames[i].Singles.ToString();
                tblCell5.Controls.Add(lblSingles);
                tblRow.Cells.Add(tblCell5);

                HtmlTableCell tblCell13 = new HtmlTableCell();
                Label lblDoubles = new Label();
                lblDoubles.Text = lstPlayerGames[i].Doubles.ToString();
                tblCell13.Controls.Add(lblDoubles);
                tblRow.Cells.Add(tblCell13);

                HtmlTableCell tblCell14 = new HtmlTableCell();
                Label lblTriples = new Label();
                lblTriples.Text = lstPlayerGames[i].Triples.ToString();
                tblCell14.Controls.Add(lblTriples);
                tblRow.Cells.Add(tblCell14);

                HtmlTableCell tblCell12 = new HtmlTableCell();
                Label lblWalks = new Label();
                lblWalks.Text = lstPlayerGames[i].Walks.ToString();
                tblCell12.Controls.Add(lblWalks);
                tblRow.Cells.Add(tblCell12);

                HtmlTableCell tblCell6 = new HtmlTableCell();
                Label lblHomeRuns = new Label();
                lblHomeRuns.Text = lstPlayerGames[i].HomeRuns.ToString();
                tblCell6.Controls.Add(lblHomeRuns);
                tblRow.Cells.Add(tblCell6);

                HtmlTableCell tblCell7 = new HtmlTableCell();
                Label lblRBI = new Label();
                lblRBI.Text = lstPlayerGames[i].RunsBattedIn.ToString();
                tblCell7.Controls.Add(lblRBI);
                tblRow.Cells.Add(tblCell7);

                HtmlTableCell tblCell8 = new HtmlTableCell();
                Label lblTotalBases = new Label();
                lblTotalBases.Text = lstPlayerGames[i].TotalBases.ToString();
                tblCell8.Controls.Add(lblTotalBases);
                tblRow.Cells.Add(tblCell8);

                HtmlTableCell tblCell9 = new HtmlTableCell();
                Label lblAverage = new Label();
                lblAverage.Text = lstPlayerGames[i].Average.ToString();
                tblCell9.Controls.Add(lblAverage);
                tblRow.Cells.Add(tblCell9);

                HtmlTableCell tblCell10 = new HtmlTableCell();
                Label lblOBP = new Label();
                lblOBP.Text = lstPlayerGames[i].OnBasePercentage.ToString();
                tblCell10.Controls.Add(lblOBP);
                tblRow.Cells.Add(tblCell10);

                HtmlTableCell tblCell11 = new HtmlTableCell();
                Label lblSlugging = new Label();
                lblSlugging.Text = lstPlayerGames[i].Slugging.ToString();
                tblCell11.Controls.Add(lblSlugging);
                tblRow.Cells.Add(tblCell11);

                HtmlTableCell tblCell15 = new HtmlTableCell();
                Label lblOPS = new Label();
                lblOPS.Text = lstPlayerGames[i].OPS.ToString();
                tblCell15.Controls.Add(lblOPS);
                tblRow.Cells.Add(tblCell15);

                tblHitters.Rows.Add(tblRow);
            }
        }

        void lnkName_Click(object sender, EventArgs e)
        {
            //got here
            LinkButton playerRow = (LinkButton)sender;
            lblName.Text = playerRow.Text;

            //get player
            SoftballStats_BL.Player thisPlayer = SoftballStats_BL.Player.GetPlayerByID(Guid.Parse(playerRow.CommandName));

            //fill picture
            imgLogo.ImageUrl = SoftballStats_BL.Player.GetPlayerImage(thisPlayer.FirstName + " " + thisPlayer.LastName);

            List<SoftballStats_BL.AtBat> lstAtBats = SoftballStats_BL.AtBat.GetAtBatsByPlayerGameID(Guid.Parse(playerRow.CommandArgument));
            HtmlTableRow tblRow = new HtmlTableRow();
            for (int i = 0; i < lstAtBats.Count; i++)
            {
                HtmlTableCell tblHeader = new HtmlTableCell();
                Label lblHeader = new Label();
                lblHeader.Text = lstAtBats[i].Inning.ToString();
                lblHeader.CssClass = "tableHeader";
                tblHeader.Controls.Add(lblHeader);
                tblRow.Cells.Add(tblHeader);
            }
            tblOutcomes.Rows.Add(tblRow);

            HtmlTableRow tblRow2 = new HtmlTableRow();
            for (int i = 0; i < lstAtBats.Count; i++)
            {
                HtmlTableCell tblCell = new HtmlTableCell();
                Image imgOutcome = new Image();
                imgOutcome.ImageUrl = GetOutcomeImageURL(lstAtBats[i].Outcome, lstAtBats[i].NumberOfRBI, lstAtBats[i].Run, lstAtBats[i].Safe);
                tblCell.Controls.Add(imgOutcome);
                tblRow2.Cells.Add(tblCell);
            }
            tblOutcomes.Rows.Add(tblRow2);

            ModalPopupExtender1.Show();
        }

        private string GetOutcomeImageURL(string outcome, short rbi, bool run, bool safe)
        {
            string path = "../Images/Outcomes/";

            if (safe)
            {
                path += "safe/";

                if (run)
                {
                    path += "run/" + outcome;
                }
                else
                {
                    path += outcome;
                }

                if (rbi > 0)
                {
                    path += rbi + "rbi";
                }

                if (run)
                {
                    path += "run";
                }
            }
            else
            {
                path += "out/" + outcome;

                if (rbi > 0)
                {
                    path += rbi + "rbi";
                }
            }

            path += ".png";

            return path;
        }
    }
}